package com.bio.test2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * 处理服务逻辑
 * @author gxkj-042
 *
 */
public class ServerHandler implements Runnable {
	
	private Socket socket;
	
	public ServerHandler(Socket socket) {
		this.socket = socket;
	}
	
	@Override
	public void run() {
		BufferedReader reader = null;
		PrintWriter writer = null;
		try {
			reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			writer = new PrintWriter(this.socket.getOutputStream(),true);
			String body = null;
			while (true) {
				body = reader.readLine();
				if(body == null){
					break;
				}
				System.out.println("服务端接收到信息："+body);
				writer.println("服务端接收完信息后，返回响应的数据！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(reader != null){
				try {
					reader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(writer != null){
				try {
					writer.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(socket != null ){
				try {
					socket.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
